#include "protocal.hpp"

enum
{
    Div_Zero = 1,
    Mod_Zero,
    Other_Oper
};

class serverCal
{
public:
    response _calculator(const request &req)
    {
        response res(0, 0);
        switch (req._op)
        {
        case '+':
            res._result = req._a + req._b;
            break;
        case '-':
            res._result = req._a - req._b;
            break;
        case '*':
            res._result = req._a * req._b;
            break;
        case '/':
        {
            if (req._b == 0)
                res._code = Div_Zero;
            else
                res._result = req._a / req._b;
        }
        break;
        case '%':
        {
            if (req._b == 0)
                res._code = Mod_Zero;
            else
                res._result = req._a % req._b;
        }
        break;
        default:
            res._code = Other_Oper;
            break;
        }
        return res;
    }

    string calculator(string &packageBuffer)
    {
        string content;
        if (!decode(packageBuffer, &content))
        {
            return "";
        }

        request req;
        if (!req.deserialize(content))
        {
            return "";
        }
        req.DebugPrint();
        response res = _calculator(req);

        content = "";
        res.serialize(&content);
        res.DebugPrint();

        return code(content);
    }
};